package com.example.movie_shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.movie_shop.Util.QueryCondition;
import com.example.movie_shop.entry.Movie;
import com.example.movie_shop.service.MovieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    //select * from t_movie limit 0,16;
    @RequestMapping("show_movies")
    public Page<Movie> get_movie_page(QueryCondition condition){
        System.out.println(condition);
        Page page = new Page(condition.getPageNew(),condition.getPageSize());
        //封装查询条件
        //1.电影类型
        QueryWrapper<Movie> wrapper = new QueryWrapper<>();
        if(condition.getGenres()!=null && !"".equals(condition.getGenres())){
            wrapper.eq("genres",condition.getGenres());
        }
        //2.电影价格
        if(condition.getPrice2()!=null && condition.getPrice2()>0){
            wrapper.between("price",condition.getPrice1(),condition.getPrice2());
        }
        //3.价格与评分排序
        String orderBy = condition.getOrderBy();
        if(orderBy!=null && !orderBy.equals("")){
            if(orderBy.equals("price")){
                wrapper.orderByAsc(orderBy);
            }else {
                wrapper.orderByDesc(orderBy);
            }
        }
        //分页查询+条件查询，如果条件为空不进行条件查询
        Page page_movies = movieService.page(page,wrapper);
        return page_movies;
    }

    @RequestMapping("a_movie")
    public Movie get_a_movie(Integer m_ID){
        return movieService.getById(m_ID);
    }

    @RequestMapping("/find_one")
    public Movie findById(Integer id){
        return movieService.getById(id);
    }
}
